package cn.ly.gateway.core.session.impl;

import cn.ly.gateway.core.datasource.Connection;
import cn.ly.gateway.core.datasource.DataSourceFactory;
import cn.ly.gateway.core.datasource.DataSource;
import cn.ly.gateway.core.datasource.unpooled.UnpooledDataSourceFactory;
import cn.ly.gateway.core.executor.Executor;
import cn.ly.gateway.core.session.Configuration;
import cn.ly.gateway.core.session.GatewaySession;
import cn.ly.gateway.core.session.GatewaySessionFactory;

/**
 * 默认网关会话工厂
 *
 * @author liuyu
 */
public class DefaultGatewaySessionFactory implements GatewaySessionFactory {

    private final Configuration configuration;

    public DefaultGatewaySessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public GatewaySession openSession(String uri) {
        DataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
        dataSourceFactory.setProperties(configuration, uri);
        DataSource dataSource = dataSourceFactory.getDataSource();
        Connection connection = dataSource.getConnection();
        // 创建执行器
        Executor executor = configuration.newExecutor(connection);
        return new DefaultGatewaySession(configuration, executor, uri);
    }

    public Configuration getConfiguration() {
        return configuration;
    }
}
